package com.asen.commons.core.base.service;

import com.asen.commons.core.base.domain.Core;
import com.asen.commons.core.base.enums.BillStatusEnum;
import com.asen.commons.core.meta.dao.Condition;

import java.math.BigDecimal;
import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 通用服务接口
 *
 * @author ls
 * @version 1.0
 * @date 2024-04-09
 */
public interface CoreService<T extends Core> {

    /**
     * 新增
     *
     * @param model
     * @throws Exception
     */
    void add(T model) throws Exception;

    /**
     * 新增
     *
     * @param model
     * @param id
     * @throws Exception
     */
    void add(T model, Long id) throws Exception;

    /**
     * 批量新增
     *
     * @param list
     * @throws Exception
     */
    void batchAdd(Collection<T> list) throws Exception;

    /**
     * 更新
     *
     * @param model
     * @throws Exception
     */
    void update(T model) throws Exception;

    /**
     * 根据condition条件更新
     *
     * @param model
     * @param condition
     * @throws Exception
     */
    void updateBy(T model, Condition condition) throws Exception;

    /**
     * 更新model对象由partialFields指定的属性字段
     *
     * @param model
     * @param partialFields
     * @throws Exception
     */
    void updatePartial(T model, String... partialFields) throws Exception;

    /**
     * 批量更新
     *
     * @param list
     * @throws Exception
     */
    void batchUpdate(Collection<T> list) throws Exception;

    /**
     * 批量更新
     *
     * @param list
     * @param partialFields
     * @throws Exception
     */
    void batchUpdatePartial(Collection<T> list, String... partialFields) throws Exception;

    /**
     * 删除
     *
     * @param model
     * @throws Exception
     */
    void remove(T model) throws Exception;

    /**
     * 删除
     *
     * @param id
     * @throws Exception
     */
    void removeBy(Long id) throws Exception;

    /**
     * 删除
     *
     * @param condition
     * @throws Exception
     */
    void removeBy(Condition condition) throws Exception;

    /**
     * 删除
     *
     * @param condition
     * @param updateTime
     * @throws Exception
     */
    void removeBy(Condition condition, boolean updateTime) throws Exception;

    /**
     * 批量删除
     *
     * @param list
     * @throws Exception
     */
    void batchRemove(Collection<T> list) throws Exception;

    /**
     * 彻底删除
     *
     * @param model
     * @throws Exception
     */
    void removeCompletely(T model) throws Exception;

    /**
     * 彻底删除
     *
     * @param condition
     * @throws Exception
     */
    void removeByCompletely(Condition condition) throws Exception;

    /**
     * 获取对象
     *
     * @param id
     * @return
     * @throws Exception
     */
    T get(Long id) throws Exception;

    /**
     * 获取对象
     *
     * @param condition
     * @return
     * @throws Exception
     */
    T getBy(Condition condition) throws Exception;

    /**
     * 查询所有
     *
     * @return
     * @throws Exception
     */
    List<T> getAll() throws Exception;

    /**
     * 分页查询所有
     *
     * @param pageNum
     * @param pageSize
     * @return
     * @throws Exception
     */
    List<T> getAllByPage(int pageNum, int pageSize) throws Exception;

    /**
     * 查询列表
     *
     * @param condition
     * @return
     * @throws Exception
     */
    List<T> getList(Condition condition) throws Exception;

    /**
     * 查询列表-数据隔离
     */
    List<T> getListIsolation(Condition condition) throws Exception;

    /**
     * 查询总数
     *
     * @return
     * @throws Exception
     */
    int count() throws Exception;

    /**
     * 查询总数
     *
     * @param condition
     * @return
     * @throws Exception
     */
    int count(Condition condition) throws Exception;

    /**
     * 查询总数
     *
     * @param condition
     * @return
     * @throws Exception
     */
    int countIsolation(Condition condition) throws Exception;

    /**
     * 根据ID检查
     *
     * @param id
     * @return
     * @throws Exception
     */
    boolean exist(Long id) throws Exception;

    /**
     * 查询包括已删除数据
     *
     * @param id
     * @return
     * @throws Exception
     */
    T getIncludeDeleted(Long id) throws Exception;

    /**
     * 查询包括已删除数据
     *
     * @param condition
     * @return
     * @throws Exception
     */
    List<T> getListIncludeDeleted(Condition condition) throws Exception;

    /**
     * 检查包括已删除数据
     *
     * @param id
     * @return
     * @throws Exception
     */
    boolean existIncludeDeleted(Long id) throws Exception;

    /**
     * 执行SQL插入或更新脚本
     *
     * @param sql
     * @return
     * @throws Exception
     */
    public int execSql(String sql) throws Exception;

    /**
     * 执行SQL查询
     *
     * @param sql
     * @return
     * @throws Exception
     */
    List<Map<String, Object>> query(String sql) throws Exception;

    /**
     * 执行SQL查询
     *
     * @param sql
     * @return
     * @throws Exception
     */
    <K> List<K> query(String sql, Class<K> clazz) throws Exception;

    /**
     * 更新状态
     *
     * @param id
     * @param status
     * @throws Exception
     */
    void updateStatus(Long id, BillStatusEnum status) throws Exception;

    /**
     * 反写数值字段
     *
     * @param map       id->数值 map
     * @param fieldName 数值累计字段,直接与map相加
     * @throws Exception
     */
    void updateDecimalField(Map<Long, BigDecimal> map, String fieldName) throws Exception;

    /**
     * 反写数值字段
     *
     * @param map            id->数值 map
     * @param fieldName      数值累计字段,直接与map相加
     * @param totalFieldName 数值总字段,用于剩余字段的计算
     * @param innerFieldName 数值剩余字段
     * @throws Exception
     */
    void updateDecimalField(Map<Long, BigDecimal> map, String fieldName, String totalFieldName, String innerFieldName) throws Exception;

    /**
     * 查询列表
     *
     * @param ids
     * @return
     * @throws Exception
     */
    List<T> getListIds(List<Long> ids) throws Exception;

    /**
     * 构建通用过滤
     */
    void buildCommonFilter(Condition condition) throws Exception;

    Class<T> getBillType();
}
